<?php

namespace app\api\Logic\Interact;

use app\api\Logic\CommonLogic;
use app\api\model\InteractModel;
use app\api\model\InteractThemeModel;
use think\Validate;

class InteractLogic
{
    /**
     * 获取互动讨论主题
     * @return void
     */
    public function getThemeList($where = [])
    {
        $themeModel = new InteractThemeModel();
        $list = $themeModel
        ->alias('a')
        ->where($where)
        ->join('interact b','a.id = b.theme_id','left')
        ->where('a.deletetime','null')
        ->field('a.id,a.title,a.content,a.image,count(b.id) as article_num')
        ->group('a.id')
        ->select();
        return $list;
    }


    /**
     * 获取互动讨论贴子列表
     *
     * @param [type] $shop_id 校区id
     * @param [type] $user_id 用户id
     * @param [type] $page    页码
     * @param [type] $limit   每页显示
     * @param integer $theme_id  主题id
     * @param integer $order_type 排序类型
     * @param array $map      自定义查询条件
     * @return array
     */
    public function getInteractList($shop_id,$user_id,$page,$limit,$theme_id=0,$order_type=1,$map=[])
    {
        $commonLogic = new CommonLogic();
        $shop_arr = [0,$shop_id];
        $where = [
            'a.status'=>1,
            'a.deletetime' => null,
            'a.shop_id'=>['in',$shop_arr]
        ];
        if($theme_id){//是否主题列表
            $where['theme_id'] = $theme_id;
        }
        $order = '';
        switch ($order_type) { //排序类型:1=普通排序,2=热帖排序
            case '1':
                $order = 'a.is_top desc,a.shop_id asc,a.weigh desc,a.createtime desc';
                break;
            case '2':
                $order = 'a.views desc,a.share_num desc,a.like_num desc,a.createtime desc,a.weigh desc';
                break;
        
        }

        $interactModel = new InteractModel();
        $res = $interactModel->alias('a')
        ->with(['comment','theme'])
        ->where($where)
        ->where($map)
        ->join('comment b',"a.id = b.relation_id and b.style = 'Interact'",'left')
        ->join('likes c',"a.id = c.relation_id and c.style = 'Interact' and c.user_id='{$user_id}' ",'left')
        ->group('a.id')
        ->order($order)
        ->field('a.id,a.shop_id,a.title,a.theme_id,a.content,a.images,a.type,a.relation_id,a.views,a.share_num,a.like_num,a.createtime,count(distinct b.id) as comment_num,count(distinct c.id) as is_like')
        ->limit(($page-1)*$limit,$limit)
        ->select()->toArray();
        if($res){
            foreach ($res as $v) {
                //获取作者头像信息
                $user_info = $commonLogic->getUserInfo($v['type'],$v['relation_id']);
                $v['nickname'] = $user_info['nickname'];
                $v['avatar'] = $user_info['avatar'];
                if($v['comment']){//存在评论 评论用户也加上昵称
                    foreach ($v['comment'] as $comment_v) {
                        $comment_user_info = $commonLogic->getUserInfo($comment_v['reply_type'],$comment_v['reply_id']);
                        $comment_v['nickname'] = $comment_user_info['nickname'];
                    }
                }   
            }
        }


        return $res;
    }


    
    /**
     * 获取贴子详情
     *
     * @param [type] $param 前端传入数据 
     * relation_id 贴子id 
     * shop_id 校区id
     * user_id 用户id
     * @return void
     */
    public function getInteractDetail($param)
    {
        $interactModel = new InteractModel();
        $commonLogic = new CommonLogic();
        $where = [
            'a.status'=>1,
            'a.deletetime' => null,
            'a.id' => $param['relation_id'],
        ];
        $user_id = $param['user_id'];

        $data = $interactModel->alias('a')
        ->with(['theme'])
        ->where($where)
        ->join('comment b',"a.id = b.relation_id and b.style = 'Interact'",'left')
        ->join('likes c',"a.id = c.relation_id and c.style = 'Interact' and c.user_id='{$user_id}' ",'left')
        ->group('a.id')
        ->field('a.id,a.shop_id,a.title,a.theme_id,a.content,a.images,a.type,a.relation_id,a.views,a.share_num,a.like_num,a.createtime,count(distinct b.id) as comment_num,count(distinct c.id) as is_like')
        ->find();



        if($data){
            //获取作者头像信息
            $user_info = $commonLogic->getUserInfo($data['type'],$data['relation_id']);

            $data['nickname'] = $user_info['nickname'];
            $data['avatar'] = $user_info['avatar'];
            // 浏览数+1
            $data->setInc('views');
            // 是否已点赞
            // $is_like = $commonLogic->isLike($param['user_id'],$param['shop_id'],$param['relation_id'],'Interact');
            // $data['is_like'] = $is_like ? true : false;
        }

        return $data;

    }

    /**
     * 写入互动讨论表
     *
     * @param [type] $param 前端传入数据
     * @return void
     */
    public function saveInteract($param)
    {
        $images = !empty($param['images']) ? implode(',',$param['images']):'';
        $res = model('InteractModel')->allowField(true)->save([
            'shop_id' => $param['shop_id'],
            'title' => $param['title'],
            'theme_id' => $param['theme_id'],
            'content' => $param['content'],
            'images' => $images,
            'weigh' => 0,
            'status' => 0,
            'type' => 2,
            'relation_id' => $param['user_id'],
        ]);
        return $res;
    }

    /**
     * 验证发布互动讨论数据
     *
     * @param [type] $param 前端传入数据
     * @return void
     */
    public function interactVerifyData($param)
    {
        $rule = [
            'title'  => 'require',
            'theme_id'  => 'require|number',
            'content'  => 'require',
        ];
        $msg = [
            'title.require' => '标题未填',
            'theme_id.require' => '主题未选',
            'theme_id.number' => '主题错误',
            'content.require' => '内容未填',
        ];
        $validate = new Validate($rule,$msg);
        if (!$validate->check($param)) {
            return $validate->getError();
        }
        return false;
    }


}
